package com.mis.service.impl.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mis.entity.biz.OrderCustomerEntity;
import com.mis.page.biz.OrderCustomerPage;
import com.mis.service.biz.OrderCustomerServiceI;

import com.jeecg.pageModel.TreeNode;
import com.jeecg.util.pojo.MyBeanUtils;
import com.jeecg.util.dbcommon.SearchSqlGenerateUtil;
import com.jeecg.service.impl.BaseServiceImpl;
import com.jeecg.pageModel.DataGrid;
import com.jeecg.dao.jdbc.JdbcDao;
import com.jeecg.dao.BaseDaoI;

/**   
 * @Title: ServiceImpl
 * @Description: 订单产品明细表
 * @author zhangdaihao
 * @date 2014-12-08 17:21:52
 * @version V1.0   
 *
 */
@Service("orderCustomerService")
public class OrderCustomerServiceImpl extends BaseServiceImpl implements OrderCustomerServiceI {

	@Autowired
	//SQL 使用JdbcDao
	private JdbcDao jdbcDao;
	private BaseDaoI<OrderCustomerEntity> orderCustomerEntityDao;

	public BaseDaoI<OrderCustomerEntity> getOrderCustomerEntityDao() {
		return orderCustomerEntityDao;
	}
	@Autowired
	public void setOrderCustomerEntityDao(BaseDaoI<OrderCustomerEntity> orderCustomerEntityDao) {
		this.orderCustomerEntityDao = orderCustomerEntityDao;
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public DataGrid datagrid(OrderCustomerPage orderCustomerPage) {
		DataGrid j = new DataGrid();
		j.setRows(getPagesFromEntitys(find(orderCustomerPage)));
		j.setTotal(total(orderCustomerPage));
		return j;
	}

	private List<OrderCustomerPage> getPagesFromEntitys(List<OrderCustomerEntity> orderCustomerEntitys) {
		List<OrderCustomerPage> orderCustomerPages = new ArrayList<OrderCustomerPage>();
		if (orderCustomerEntitys != null && orderCustomerEntitys.size() > 0) {
			for (OrderCustomerEntity tb : orderCustomerEntitys) {
				OrderCustomerPage b = new OrderCustomerPage();
				BeanUtils.copyProperties(tb, b);
				orderCustomerPages.add(b);
			}
		}
		return orderCustomerPages;
	}

	private List<OrderCustomerEntity> find(OrderCustomerPage orderCustomerPage) {
		String hql = "from OrderCustomerEntity t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderCustomerPage, hql, values);

		if (orderCustomerPage.getSort() != null && orderCustomerPage.getOrder() != null) {
			hql += " order by " + orderCustomerPage.getSort() + " " + orderCustomerPage.getOrder();
		}
		return orderCustomerEntityDao.find(hql, orderCustomerPage.getPage(), orderCustomerPage.getRows(), values);
	}

	private Long total(OrderCustomerPage orderCustomerPage) {
		String hql = "select count(*) from OrderCustomerEntity t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderCustomerPage, hql, values);
		return orderCustomerEntityDao.count(hql, values);
	}

	private String addWhere(OrderCustomerPage orderCustomerPage, String hql, List<Object> values) {
	    //循环查询条件Page的所有[Integer][String]类型的字段，如果字段不为空则动态加上查询条件
		//-----------------------------------------------------
		StringBuffer hqlbf = new StringBuffer(hql);
		
		OrderCustomerEntity orderCustomerEntity = new OrderCustomerEntity();
		BeanUtils.copyProperties(orderCustomerPage, orderCustomerEntity);
		SearchSqlGenerateUtil.createSearchParamsHql(hqlbf, values, orderCustomerEntity);
		hql = hqlbf.toString();
		return hql;
	}

	public void add(OrderCustomerPage orderCustomerPage) {
		OrderCustomerEntity t = new OrderCustomerEntity();
		BeanUtils.copyProperties(orderCustomerPage, t);
		orderCustomerEntityDao.save(t);
	}

	public void update(OrderCustomerPage orderCustomerPage) throws Exception {
		OrderCustomerEntity t = orderCustomerEntityDao.get(OrderCustomerEntity.class, orderCustomerPage.getObid());
	    if(t != null) {
			MyBeanUtils.copyBeanNotNull2Bean(orderCustomerPage, t);
		}
	}

	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				String hql = "from OrderCustomerEntity where obid = '"+id+"'";
				OrderCustomerEntity t = orderCustomerEntityDao.get(hql);
				if (t != null) {
					orderCustomerEntityDao.delete(t);
				}
			}
		}
	}

	public OrderCustomerEntity get(OrderCustomerPage orderCustomerPage) {
		return orderCustomerEntityDao.get(OrderCustomerEntity.class, orderCustomerPage.getObid());
	}

	public OrderCustomerEntity get(java.lang.String obid) {
		return orderCustomerEntityDao.get(OrderCustomerEntity.class, obid);
	}
	public List<OrderCustomerEntity> listAll(OrderCustomerPage orderCustomerPage) {
		String hql = "from OrderCustomerEntity where 1 = 1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderCustomerPage, hql, values);
		List<OrderCustomerEntity> list = orderCustomerEntityDao.find(hql,values);
		return list;
	}
}
